package com.cn.why.produce_pro.service;

import com.cn.why.film.filmindex.entity.PetProduce;
import com.cn.why.produce_pro.rabbit.ProducerMsg;
import com.cn.why.produce_pro.repository.ProduceDao;
import io.swagger.models.auth.In;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 库存系统操作
 */
@Component
@RabbitListener(queues = "QUEUE_A")
public class Oder {

    @Autowired
    private ProduceDao produceDao;
    @Autowired
    private ProducerMsg producerMsg;
    @RabbitHandler
    public void process(Map<String, Object> content){
        //返回的消息
        Map<String,Object> map = new HashMap<>();
        //查询库存系统,MQ异步更新
        PetProduce produce = produceDao.findOne((Integer) content.get("id"));
        Long count = produce.getCount();
        synchronized (this){
            if(count > ((Integer)content.get("number"))){
                //修改数据库
                produceDao.updataNuber((Integer)content.get("number"),(Integer) content.get("id"));
                map.put("msg","suceess");
                map.put("produce",produce);
            }else {
                map.put("msg","error");
            }
        }
        //MQ发送反馈消息
        producerMsg.sendMsg(map,"EXCHANGE_A","ROUTINGKEY_B");
    }
}
